//
//  LYFindViewController.m
//  音伴尔家听宝
//
//  Created by Burgess on 2017/4/7.
//  Copyright © 2017年 Burgess. All rights reserved.
//

#import "LYCollectionviewOnTableviewCell.h"
#import "LYFindViewController.h"
#import "LYcloudsclassview.h"
#import "LYPageView.h"
#import "LYcellview.h"
#import "LYmainPageModel.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImage+AFNetworking.h>
#import <UIImageView+AFNetworking.h>
#import "LYdownCellView.h"
#import "LYMusicBtnWindowSingleton.h"
#import "LYCollectionCell.h"
#import <MJRefresh.h>
#import <UIButton+WebCache.h>
#import <UIImageView+WebCache.h>
#import "LYClassInfoViewController.h"


@interface LYFindViewController () <UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate  >
//  接口数据
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) NSArray *interfaceData1;
@property (nonatomic, strong) NSArray *interfaceData2;
@property (nonatomic, strong) NSArray *interfaceData3;
@property (nonatomic, strong) NSArray *interfaceData4;

@property (nonatomic, strong) NSArray *catlistmodel;

@property (strong,nonatomic) LYmainPageModel * model;

@property (strong ,nonatomic) UITableView * tabelview;

@property (strong , nonatomic) LYMusicBtnWindowSingleton * windowly;

@end

@implementation LYFindViewController


-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setBackgroundImage: NULL forBarMetrics:UIBarMetricsDefault];
}

-(void)mainPageModel{

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:config];
    [manager GET:@"http://cute.fcloudclass.com/api.php/Homepage/home" parameters:NULL success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        self.interfaceData1 = [LYmainPageModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"advertisement"]];
        self.interfaceData2 = [LYmainPageModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"recomment"]];

        self.interfaceData3 = @[self.interfaceData2[3],self.interfaceData2[4],self.interfaceData2[5]];
        self.interfaceData4 = @[self.interfaceData2[6],self.interfaceData2[7],self.interfaceData2[8]];

        self.data = @[self.interfaceData2,self.interfaceData3,self.interfaceData4];
      
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tabelview reloadData];
            [self.tabelview.mj_header endRefreshing];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"left_icon"]  style:UIBarButtonItemStyleDone target:self action:NULL];
    self.navigationItem.title = @"智能学习电台";
    
    NSThread * myFirstThread = [[NSThread alloc]initWithTarget:self selector:@selector(mainPageModel) object:nil];
    [myFirstThread start];

    self.windowly = [LYMusicBtnWindowSingleton sharedMusicBtnWindowSingleton:self];
    self.windowly.windowLevel = UIWindowLevelNormal+3;
    self.windowly.frame = CGRectMake((SCREEN_WIDTH-75)/2, (SCREEN_HEIGHT-75), 75, 75);
    self.windowly.backgroundColor = [UIColor clearColor];
    self.windowly.hidden = NO;
    
    CGRect rect1 = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.view.frame.size.height-49);
     _tabelview = [[UITableView alloc]initWithFrame:rect1 style:UITableViewStyleGrouped];
    _tabelview.dataSource  =  self;
    _tabelview.delegate      =  self;
    _tabelview.showsVerticalScrollIndicator = NO;
    [self.view addSubview: _tabelview];
    
}


#pragma mark - TableView数据源方法和代理方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.data.count == 0) {
        return 0;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
    return self.data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        LYCollectionviewOnTableviewCell * cellll = [tableView dequeueReusableCellWithIdentifier:@"ddd"];
        if (cellll == NULL) {
            cellll = [[LYCollectionviewOnTableviewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"ddd"  null:NULL];
        }
    LYCollectionviewOnTableviewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"sss"];
    if (cell  ==  NULL) {
        cell  =  [[LYCollectionviewOnTableviewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"sss"];
    }
        if (indexPath.row == 0) {
            [cellll setvaluetoarraymodel:_interfaceData2 tableviewcell:cellll viewcontroller:self cellIndex:indexPath.row];
            return cellll;
        }
        else if  (indexPath.row == 1){
            [cell setvaluetoarraymodel:_interfaceData3 tableviewcell:cell viewcontroller:self cellIndex:indexPath.row];
            return cell;
        }else{
            [cell setvaluetoarraymodel:_interfaceData4 tableviewcell:cell viewcontroller:self cellIndex:indexPath.row];
            return cell;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {

        LYPageView * pageview = [LYPageView pageView];

            LYmainPageModel* model1 = _interfaceData1[0];
            LYmainPageModel* model2 = _interfaceData1[1];
//            LYmainPageModel* model3 = _interfaceData1[2];

            pageview.imageWebNames = @[model1.mob_large_img,model2.mob_large_img];

        pageview.bounds = CGRectMake(0, 0, SCREEN_WIDTH, 170);
 
        return pageview;

    }else
        return NULL;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

        return 170;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        return (SCREEN_WIDTH-44)/3+35+20+30;
    }
        return (SCREEN_WIDTH-44)/3+20+35;
}

-(BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{

        return NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LYClassInfoViewController *tableVc = [[LYClassInfoViewController alloc]init];
    [self.navigationController pushViewController:tableVc animated:YES];
  
}

@end
